library(polycor)
source("Code/utility functions.R")
anes2012$oldnew
anes2012$oldnew_both <-anes2012$oldnew
anes2012$oldnew_both[anes2012$oldnew!=anes2012$oldnew2]=NA


ef1s <- with(subset(anes2012,is.na(oldnew_both)==F), hetcor(na.omit(data.frame(as.ordered(efficacy1stdpo),as.ordered(efficacy1stdpre))))$correlations[2,1])
ef1r <- with(subset(anes2012,is.na(oldnew_both)==F), hetcor(na.omit(data.frame(as.ordered(efficacy1revpo),as.ordered(efficacy1revpre))))$correlations[2,1])

ef2s <- with(subset(anes2012,is.na(oldnew_both)==F), hetcor(na.omit(data.frame(as.ordered(efficacy2stdpo),as.ordered(efficacy2stdpre))))$correlations[2,1])
ef2r <- with(subset(anes2012,is.na(oldnew_both)==F), hetcor(na.omit(data.frame(as.ordered(efficacy2revpo),as.ordered(efficacy2revpre))))$correlations[2,1])

ef3s <- with(subset(anes2012,is.na(oldnew_both)==F), hetcor(na.omit(data.frame(as.ordered(efficacy3stdpo),as.ordered(efficacy3stdpre))))$correlations[2,1])
ef3r <- with(subset(anes2012,is.na(oldnew_both)==F), hetcor(na.omit(data.frame(as.ordered(efficacy3revpo),as.ordered(efficacy3revpre))))$correlations[2,1])

ef4s <- with(subset(anes2012,is.na(oldnew_both)==F), hetcor(na.omit(data.frame(as.ordered(efficacy4stdpo),as.ordered(efficacy4stdpre))))$correlations[2,1])
ef4r <- with(subset(anes2012,is.na(oldnew_both)==F), hetcor(na.omit(data.frame(as.ordered(efficacy4revpo),as.ordered(efficacy4revpre))))$correlations[2,1])

external_s <- with(subset(anes2012,is.na(oldnew_both)==F), hetcor(na.omit(data.frame(as.ordered(stdpre_external),as.ordered(stdpo_external))))$correlations[2,1])
external_r <- with(subset(anes2012,is.na(oldnew_both)==F), hetcor(na.omit(data.frame(as.ordered(revisedpre_external),as.ordered(revisedpo_external))))$correlations[2,1])

internal_s <- with(subset(anes2012,is.na(oldnew_both)==F), hetcor(na.omit(data.frame(as.ordered(stdpre_internal),as.ordered(stdpo_internal))))$correlations[2,1])
internal_r <- with(subset(anes2012,is.na(oldnew_both)==F), hetcor(na.omit(data.frame(as.ordered(revisedpre_internal),as.ordered(revisedpo_internal))))$correlations[2,1])


anes2012$stdpo_externalpo_external
reliabilityanalysis <- rbind(c(ef1s,ef1r,ef1s-ef1r),c(ef2s,ef2r,ef2s-ef2r),c(ef3s,ef3r,ef3s-ef3r),c(ef4s,ef4r,ef4s-ef4r),c(external_s,external_r,external_s-external_r),c(internal_s,internal_r,internal_s-internal_r))
rownames(reliabilityanalysis) <- c("Efficacy 1","Efficacy 2","Efficacy 3","Efficacy 4","External Efficacy","Internal Efficacy")
colnames(reliabilityanalysis) <- c("Standard","Revised","Difference")

save(reliabilityanalysis,file="./output/reliabilityanalysisagreeable.RData")

library(xtable)   
sink("output/reliability1.txt")
print(xtable(reliabilityanalysis,caption="Test-Retest Reliability Estimates for Agree-Disagree Item and Construct-Specific Items",align="lccc"),caption.placement="top")
sink()
